﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormProject.Diger
{
    public partial class IlTanitimKarti : Form
    {
        public IlTanitimKarti()
        {
            InitializeComponent();
        }

        DBBaglantiDataContext db = new DBBaglantiDataContext();
        public BindingSource MyNewSource = null;
        public DataSet MyDataSet = new DataSet();
        private void IlTanitimKarti_Load(object sender, EventArgs e)
        {
            IlDoldur();
        }

        public DataTable IlGetir()
        {
            var sorgu = (from x in db.Sehirlers select x);
            SqlCommand cmd = db.GetCommand(sorgu) as SqlCommand;
            DataTable dt = new DataTable("Sehirler");
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            return dt;
        }
        private void IlDoldur()
        {

            if (MyDataSet.Tables["Sehirler"] != null)
            { 
                 MyDataSet.Tables.Remove("Sehirler");
            }
            MyDataSet.Tables.Add(IlGetir());
            MyNewSource = new BindingSource();
            MyNewSource.DataSource = MyDataSet;
            MyNewSource.DataMember = MyDataSet.Tables["Sehirler"].TableName;
            DGWSehirler.DataSource = MyNewSource;
            MyNewSource.Position = MyNewSource.Count;
            bindingNavigator1.BindingSource = MyNewSource;
            MyNewSource.PositionChanged += new EventHandler(MyNewSource_PositionChanged);

        }

        void MyNewSource_PositionChanged(object sender, EventArgs e)
        {

            try
            {
                txtSehirID.DataBindings.Clear();
                SehirAdi.DataBindings.Clear();
                txtSehirID.DataBindings.Add("Text", MyNewSource, "SehirID");
                SehirAdi.DataBindings.Add("Text", MyNewSource, "SehirAdi");
            }
            catch (Exception)
            {
                
                throw;
            }
           
        }

        private void SehirAdi_ara_TextChanged(object sender, EventArgs e)
        {
            if ( SehirAdi_ara.Text.Trim().Length > 0)
            {

                string filtre = "SehirAdi like '%" + SehirAdi_ara.Text + "%'";
                MyNewSource.Filter = filtre;
                MyNewSource.Position = MyNewSource.Count - 1;
            }
            else
            {
                MyNewSource.RemoveFilter();
                MyNewSource.Position = MyNewSource.Count - 1;
            }
        }

        private void bt_kaydet_Click(object sender, EventArgs e)
        {
            Sehirler sh = new Sehirler();
            var Kayitkontrol = (from x in db.Sehirlers where x.SehirID == Convert.ToInt32(txtSehirID.Text) select x).FirstOrDefault();

            if (Kayitkontrol != null)
            {

                Kayitkontrol.SehirAdi = SehirAdi.Text;
                db.SubmitChanges();
                MessageBox.Show("Kayıt Güncellendi");
                IlDoldur();
                return;
            }
            else
            {
                sh.SehirID =Convert.ToInt32(txtSehirID.Text);
                sh.SehirAdi = SehirAdi.Text;
                db.Sehirlers.InsertOnSubmit(sh);
                db.SubmitChanges();
                IlDoldur();
            }
        }

        private void bt_yeni_Click(object sender, EventArgs e)
        {
            if (MyNewSource.Filter != string.Empty)
            {
                MyNewSource.RemoveFilter();            
            }           
                MyNewSource.AddNew();
                txtSehirID.Text = string.Empty;
                SehirAdi.Text = string.Empty;
                txtSehirID.FindForm();
            

        }

       

    }
}
